using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Chart;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;
using System.Diagnostics;

namespace FilteredStatistics.Common.Controller
{
    class ElevationDataTrackProvider : IActivityDataTrackProvider
    {
        public ElevationDataTrackProvider()
        {
            m_ActivityDataChangedHelper.PropertyChanged += new PropertyChangedEventHandler(OnActivityDataChanged);
        }

        void OnActivityDataChanged(object sender, PropertyChangedEventArgs e)
        {
            if (DataTrackChanged != null)
            {
                bool trackNeedsRefresh = false;
                bool axisNeedsRefresh = false;
                bool stripesNeedsRefresh = false;

                if (e.PropertyName == "Activity.Duration")
                {
                    trackNeedsRefresh = true;
                }
                else if (e.PropertyName == "ActivityCategory.ElevationUnits")
                {
                    trackNeedsRefresh = true;
                    axisNeedsRefresh = true;
                    stripesNeedsRefresh = true;
                }
                else if (e.PropertyName == "ActivityCategory.UseSystemLengthUnits")
                {
                    trackNeedsRefresh = true;
                    axisNeedsRefresh = true;
                }
                else if (e.PropertyName == "AnalysisSettings.ElevationSmoothingSeconds")
                {
                    trackNeedsRefresh = true;
                }

                // Throw events
                if (trackNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("SmoothedDataTrack"));
                }

                if (axisNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("Axis"));
                }

                if (stripesNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("AxisStripes"));
                }
            }
        }

#region IActivityDataTrackProvider Members

        public Guid ReferenceId
        {
            get { return new Guid("7774ce85-5598-44f4-b206-da0dfbec39d9"); }
        }

        public bool IsPrimaryDataTrack
        {
            get { return true; }
        }

        public IActivity Activity
        {
            set
            {
                if (m_Activity != value)
                {
                    m_Activity = value;
                    m_ActivityDataChangedHelper.Activity = value;
                }
            }
        }

        public string YAxisLabel
        {
            get
            {
                return CommonResources.Text.LabelElevation;
            }
        }

        public string YAxisUnitLabel
        {
            get
            {
                if (m_Activity != null)
                {
                    return "(" + Length.LabelPlural(m_Activity.Category.ElevationUnits) + ")";
                }

                return String.Empty;
            }
        }

        public Color TrackColor
        {
            get { return Color.FromArgb(143, 89, 2); }
        }

        public IAxisFormatter AxisFormatter 
        {
            get
            {
                return new Formatter.General();
            }
        }

        public INumericTimeDataSeries SmoothedDataTrack
        {
            get
            {
                if (m_Activity != null)
                {
                    INumericTimeDataSeries tempResult = ActivityInfoCache.Instance.GetInfo(m_Activity).SmoothedElevationTrack;

                    // Value is in meters so convert to the right unit
                    INumericTimeDataSeries result = new NumericTimeDataSeries();
                    foreach (ITimeValueEntry<float> entry in tempResult)
                    {
                        double temp = Length.Convert(entry.Value, Length.Units.Meter, m_Activity.Category.ElevationUnits);

                        result.Add(tempResult.EntryDateTime(entry), (float)temp);
                    }

                    return result;
                }

                return null;
            }
        }

        public IList<Guid> DataTrackZoneGuids
        {
            get
            {
                return null;
            }
        }

        public Guid DefaultZone
        {
            get
            {
                return Guid.Empty;
            }
        }

        public string GetDataTrackZoneName(Guid zoneId)
        {
            return String.Empty;
        }

        public IList<AxisStripe> GetDataTrackZones(Guid zoneId)
        {
            return null;
        }

        public event PropertyChangedEventHandler DataTrackChanged;

#endregion

        private IActivity m_Activity = null;
        private ActivityDataChangedHelper m_ActivityDataChangedHelper = new ActivityDataChangedHelper(null);
    }
}
